<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Thongtinkh extends CI_Controller {

	function __construct(){
			parent::__construct();
			$this->load->model('Thongtinkh_model');
			$this->load->library('form_validation');
			$this->load->helper('form');
			$this->load->helper(array('my_data'));
			$this->load->model("Center_model");
			$this->load->model('Login_model');
	}

	public function khachhang()
	{
		 $tentk= $this->session->userdata('tendangnhap');
		 if(empty($tentk))
		 redirect("center/index");
		 if($this->session->userdata("quyen")=="khachhang")
		{
			$data="Xem thong tin ca nhan";
			$data1["item"]=$this->Thongtinkh_model->get_taikhoan($tentk);
			$data2["item"]= $this->Center_model->menu();
			$this->load->view('outline/dangnhap_thanhcong');		
			$this->load->view('outline/header',$data2);
			$this->load->view("khachhang", $data1);
			$this->load->view('outline/footer',$data2);
	
		}
		}
/*---------------Xem thong tin ca nhan-------------------*/
	public function xem_ttcn()
	{
		$tentk = $this->session->userdata('tendangnhap');
		if(empty($tentk))
			redirect("center/index");	
		if($this->session->userdata("quyen")=="khachhang")
		{
			$data["item"]="Xem thong tin ca nhan";
			$data1["item"]=$this->Thongtinkh_model->get_taikhoan($tentk);
			
			$data2["item"]= $this->Center_model->menu();
			$this->load->view('outline/dangnhap_thanhcong');		
			$this->load->view('outline/header',$data2);
			$this->load->view('khachhang');
			$this->load->view('xemttcn',$data1);
			$this->load->view('outline/footer',$data2);
			
		}
		else
			redirect("center/index");	
	}
/*---------------Cap nhat thong tin ca nhan-------------------*/
	public function update_ttcn()
	{
		$tentk = $this->session->userdata('tendangnhap');
		if(empty($tentk))
			redirect("center/index");	
		if($this->session->userdata("quyen")=="khachhang")
		{
			$data["item"]="Cập nhật thông tin cá nhân";
			$data1["item"]=$this->Thongtinkh_model->get_taikhoan($tentk);
			
			$data2["item"]= $this->Center_model->menu();
			$this->load->view('outline/dangnhap_thanhcong');		
			$this->load->view('outline/header',$data2);
			$this->load->view('khachhang');
			$this->load->view('capnhatttcn',$data1);
			$this->load->view('outline/footer',$data2);
			
		}
		else
			redirect("center/index");	
	}
	
/*---------------Kiem tra cap nhat thong tin ca nhan-------------------*/	
	public function update_check()
		{
   			$tentk = $this->session->userdata('tendangnhap');
			if(empty($tentk))
				redirect("center/index");	
			if($this->session->userdata("quyen")=="khachhang")
			{
				$email = $this->input->post('txtemail');
				//if(!$this->Login_model->check_email($email))
				//{
    				$data =array(
     					'ho_ten' => $this->input->post('txthoten'),
						'ngay_sinh' =>$this->input->post('txtngaysinh'),
						'gioi_tinh' => $this->input->post('rdGioiTinh'),
						'dia_chi' => $this->input->post('txtdiachi'),
						'email' => $email,
						'sdt' => $this->input->post('txtsodienthoai'),
						'cmnd' => $this->input->post('txtsocmnd'),
    				);
   					$this->Thongtinkh_model->update_ttkh($tentk,$data);
    				redirect("thongtinkh/xem_ttcn");   
				//}
			}
				else
			{
				redirect("center/index");
			}
		}
/*---------------Doi mat khau-------------------*/	

	public function doimatkhau()
	{
		$tentk = $this->session->userdata('tendangnhap');
		if(empty($tentk))
			redirect("center/index");	
		if($this->session->userdata("quyen")=="khachhang")
		{
			$data["item"]="Đổi mật khẩu";		
			$data1["item"]= $this->Center_model->menu();
			$this->load->view('outline/dangnhap_thanhcong');		
			$this->load->view('outline/header',$data1);
			$this->load->view('khachhang');
			$this->load->view('doimatkhau');
			$this->load->view('outline/footer',$data1);
			
		}
		else
			redirect("center/index");	
	}
/*---------------Kiem tra doi mat khau-------------------*/	
	
	public function check_doimk()
	{
		$tentk = $this->session->userdata('tendangnhap');
		if(empty($tentk))
			redirect("center/index");
		$this->load->library('form_validation');	
		$data1["item"]= $this->Center_model->menu();
		if($this->session->userdata("quyen")=="khachhang")
		{
			if($this->form_validation->run('doimk')==false)
			{
				$this->load->view('outline/dangnhap_thanhcong');		
				$this->load->view('outline/header',$data1);
				$this->load->view('khachhang');
				$this->load->view('doimatkhau');
				$this->load->view('outline/footer',$data1);
				
			}
			else
			{
				//$data2 = array(
			     //      'matkhaucu'=> $this->input->post('matkhaucu_doimatkhau'),
					//   'nhaplaimatkhau' =>$this->input->post('nhaplaimatkhaumoi_doimatkhau')
					//	);
				$matkhaucu=$this->input->post('matkhaucu_doimatkhau');
				$nhaplaimatkhau=$this->input->post('nhaplaimatkhaumoi_doimatkhau');
				$matkhaumoi = $this->input->post('matkhaumoi_doimatkhau');
				$data3['matkhaumoi']= $this->input->post('matkhaumoi_doimatkhau');	
				$data4= $this->Thongtinkh_model->get_taikhoan($tentk);
				if ((strcasecmp($matkhaucu,$this->encrypt->decode($data4->mat_khau,'nhi'))==0) and (strcasecmp($matkhaumoi,$nhaplaimatkhau)==0))
				{
					$update = array(
                             "mat_khau" => $this->encrypt->encode($matkhaumoi,'nhi'),
                                );
						$this->Thongtinkh_model->change_pass($tentk,$update);
						echo "<script> alert('Đổi mật khẩu thành công') </script>";
						$this->khachhang();
							
				}
				else
				{
					echo "<script> alert('Đổi mật khẩu thất bại') </script>";
						$this->doimatkhau();
				}
			}
		}
			
		}
/*---------------Xem lich su giao dich-------------------*/	
		public function xemlichsu_dattour()
		{
			$tentk = $this->session->userdata('tendangnhap');
			if(empty($tentk))
				redirect("center/index");	
			if($this->session->userdata("quyen")=="khachhang")
			{
				$data["item"]="Xem lịch sử đặt tour";		
				$data1["item"]= $this->Center_model->menu();
				$data2["item"]=$this->Thongtinkh_model->get_lichsudattour($tentk);
				$this->load->view('outline/dangnhap_thanhcong');		
				$this->load->view('outline/header',$data1);
				$this->load->view('khachhang');
				$this->load->view('xemlichsu_giaodich',$data2);
				$this->load->view('outline/footer',$data1);
			
		}
		else
			redirect("center/index");	
	}
	/*---------------Goi phan hoi-------------------*/	
		public function goiphanhoi()
		{
			$tentk = $this->session->userdata('tendangnhap');
			if(empty($tentk))
				redirect("center/index");	
			if($this->session->userdata("quyen")=="khachhang")
			{
				$data["item"]="Gởi phản hồi";	
				$data1["item"]= $this->Center_model->menu();
				$this->load->view('outline/dangnhap_thanhcong');		
				$this->load->view('outline/header',$data1);
				$this->load->view('khachhang');
				$this->load->view('phanhoi');
				$this->load->view('outline/footer',$data1);
			
		}
		else
			redirect("center/index");	
		}
		
		public function check_insertph()
		{
			$tentk = $this->session->userdata('tendangnhap');
			$ngay = getdate();
			$currentday = $ngay["year"]."-".$ngay["mon"]."-".$ngay["mday"];
			if(empty($tentk))
				redirect("center/index");	
			if($this->session->userdata("quyen")=="khachhang")
			{
				if($this->form_validation->run('phanhoi')==false)
				{
					$this->goiphanhoi();	
				}
				else
				{
					$data=array(
						'tieu_de' =>$this->input->post("txttieude"),
						'noi_dung'=>$this->input->post("txtphanhoi"),
						'ten_tk'=>$tentk,
						'ngay_gui'=>$currentday
					);
					$this->Thongtinkh_model->insert_phanhoi($data,$tentk);
					echo "<script> alert('Gởi phản hồi thành công') </script>";
					$this->goiphanhoi();
				
				}
			}
			else
			{
				
			redirect("center/index");
				
		}
		}

	}